# @Time    :2025/4/16 10:32
def max_subarray(nums):
    res, f = nums[0], 0  # 初始化 结果、累加和
    for i in nums:
        if f < 0:
            f = 0  # 如果累加和小于0，就返回到初始值0
        # 累加子数组
        f += i
        if res < f:
            res = f
    return res
nums = [-2,1,-3,4,-1,2,1,-5,4]
print(max_subarray(nums))